package com.bcsq.todolistdemo.demo;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.bcsq.todolistdemo.R;
import com.bcsq.todolistdemo.camera.CameraActivity;

import androidx.appcompat.widget.Toolbar;

public class AddGroupActivity extends AppCompatActivity implements View.OnClickListener {
    Toolbar toolbar;
    RelativeLayout rlIconChoose;
    Context mContext;

    ImageView ivChooseIcon;

    static int REQUEST_CODE = 1000;
    int chooseIconId = -1;
    EditText etGroupName;
    String groupName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_group);
        mContext = this;

        init();
    }

    private void init() {
        toolbar = findViewById(R.id.toolbar);
        rlIconChoose = findViewById(R.id.rl_icon_choose);
        etGroupName = findViewById(R.id.et_group_name);

        ivChooseIcon = findViewById(R.id.iv_choose_icon);
        toolbar.setTitle("添加分组");
        setSupportActionBar(toolbar);

        rlIconChoose.setOnClickListener(this);
        etGroupName.setOnClickListener(this);

    }

    //菜单
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //将菜单注入
        getMenuInflater().inflate(R.menu.menu_confirm, menu);
        return super.onCreateOptionsMenu(menu);
    }


    //菜单点击事件

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int menuId = item.getItemId();
        switch (menuId){
            case R.id.item_confirm ://添加列表
                groupName = etGroupName.getText().toString().trim();
                if (groupName == null || groupName.equals("")){
                    Toast.makeText(mContext, "请输入分组名", Toast.LENGTH_SHORT).show();
                    return true;
                }

                if (chooseIconId == -1){
                    Toast.makeText(mContext, "请选择id", Toast.LENGTH_SHORT).show();
                    return true;
                }

                saveGroup();
                finish();//关闭页面
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 保存分组
     */
    private void saveGroup() {
        SharedPreferences sharedPreferences = getSharedPreferences("groups", MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putInt(groupName, chooseIconId);
        edit.commit();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case  R.id.rl_icon_choose :
                Intent intent = new Intent(mContext, IconChooseActivity.class);
                //用法同startActivity基本相同，都是打开一个新的页面
                startActivityForResult(intent, REQUEST_CODE);
//                finish();
                break;
            case R.id.btn_openCamera:

                Intent intent1 = new Intent(mContext, CameraActivity.class);
                startActivity(intent1);
        }
    }

    /**
     * 当前页面的子页面点击后的数据回调
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {


        if (requestCode == REQUEST_CODE){
            if (resultCode == RESULT_OK){
                chooseIconId = data.getIntExtra("chooseIconId", -1);
                ivChooseIcon.setImageResource(chooseIconId);
//                Toast.makeText(mContext, "chooseIconId" + chooseIconId, Toast.LENGTH_LONG);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);

    }
}
